<?php

namespace app\api\controller;

use app\admin\model\Project;
use app\admin\model\Type;
use app\common\controller\Api;
use app\common\helpers\ListHelper;
use app\common\handler\TimeHandler;
use fast\Random;
use think\db\Expression;
use think\db\Query;
use think\Model;

/**
 * 数据看板
 */
class Databoard extends Api
{
    protected $noNeedLogin = ['topType','typeList','finishNum','trend'];
    protected $noNeedRight = '*';

    /**
     * 项目分类比
     */
    public function topType()
    {
        $top_num = $this->request->param("top_num",6);
        $start_date = $this->request->param("start_date");
        $end_date = $this->request->param("end_date");

        // if($start_date) $where['upload_time'] = ['>=',date('Y-m-d 00:00:00',strtotime($start_date))];
        // if($end_date) $where['upload_time'] = ['<=',date('Y-m-d 23:59:59',strtotime($end_date))];
        // $where['is_del'] = 0;
        $type_list =  Type::all();
        $data = [];
        if($type_list){
           $model = (new Project())->field('type_id ,count(1) as num');
           $model->where('is_del','0');
           if($start_date){
                $model->where('upload_time','>=',date('Y-m-d 00:00:00',strtotime($start_date)));
            }
            if($end_date){
                $model->where('upload_time','<=',date('Y-m-d 23:59:59',strtotime($end_date)));
            }
            $list=$model->group('type_id')
                ->order('num desc')
                ->select();
            $list = array_column($list,'num','type_id');

            foreach ($type_list as &$v){
                $v['num'] = isset($list[$v['id']]) ? $list[$v['id']] : 0;
            }
            $post = array_column($type_list, 'num');
            array_multisort($post, SORT_DESC, $type_list);

            $data =ListHelper::getRatio($type_list,'num','id','rate',0);
            $totalAll = array_sum(array_column($type_list,'num'));
            $rate = 0;
            $total = 0;
            if(count($data) > ($top_num-1) && ($top_num !=1)){
                $data =  array_slice($data,0,$top_num - 1);
                $total = array_sum(array_column($data,'num'));
                $rate = array_sum(array_column($data,'rate'));
            }
            array_push($data,[
                'type_name' =>'其他',
                'num'=>  $totalAll - $total,
                'rate'=> ($rate > 0 && (100 - $rate) > 0) ? 100 - $rate : 0
            ]);
        }
        $this->success(__('获取成功'),$data);
    }

    /**
     * 项目完成数
     */
    public function finishNum()
    {
        $start_date = $this->request->param("start_date");
        $end_date = $this->request->param("end_date");
        if($start_date) $where['upload_time'] = ['>=',date('Y-m-d 00:00:00',strtotime($start_date))];
        if($end_date) $where['upload_time'] = ['<=',date('Y-m-d 23:59:59',strtotime($end_date))];

        $finish_total = (new Project())->where(['is_del'=> 0])->count();
        $this->success(__('获取成功'),$finish_total);

    }


    /**
     * 项目增长量
     */
    public function trend()
    {

        $today = date('Y-m-d');
        $yesterday =  TimeHandler::getChainComparison(date('Y-m-d'),'D','Y-m-d');


        $tofay_list =(new Project())
            ->field([
                "DATE_FORMAT(upload_time,'%H') as date"
                ,"count(1) as num "
            ])
//
            ->where(['is_del'=>'0'])
            ->whereRaw("DATE_FORMAT(upload_time,'%Y-%m-%d') = "."'$today'")
            ->group('date')
            ->select();


        $yesterday_list =(new Project())
            ->field([
                "DATE_FORMAT(upload_time,'%H') as date"
                ,"count(1) as num "
            ])
            ->where(['is_del'=>'0'])
            ->whereRaw("DATE_FORMAT(upload_time,'%Y-%m-%d') = "."'$yesterday'")
            ->group('date')
            ->select();
        $tofay_list  = array_column($tofay_list,'num','date');

        $yesterday_list  = array_column($yesterday_list,'num','date');
        $time_arr = \app\common\common\Common::$time_arr;

        $data = [];
        foreach ($time_arr as $k=>$v){
            $num1 = 0;
            $num2 = 0;
            if($tofay_list || $yesterday_list){
                foreach ($v as $vs){
                    $num1 = $num1 + ($tofay_list[$vs] ?? 0 );

                    $num2 = $num2 + ($yesterday_list[$vs] ?? 0 );
                }
            }
            $data[]=[
                'date'=>$k,
                'num1' =>$num1,
                'num2' =>$num2,
            ];
        }
        $this->success(__('获取成功'),$data);
    }


    /**
     * 类型分布
     */
    public function typeList()
    {
        $start_date = $this->request->param("start_date");
        $end_date = $this->request->param("end_date");
        if($start_date) $where['upload_time'] = ['>=',date('Y-m-d 00:00:00',strtotime($start_date))];
        if($end_date) $where['upload_time'] = ['<=',date('Y-m-d 23:59:59',strtotime($end_date))];
        $where['is_del'] = 0;
        $type_list =  Type::all();

        $data = [];
        if($type_list){
            $list = (new Project())
                ->field('type_id ,count(1) as num')
                ->where($where)
                ->group('type_id')
                ->order('num desc')
                ->select();
            $list = array_column($list,'num','type_id');

            foreach ($type_list as &$v){
                $v['num'] = isset($list[$v['id']]) ? $list[$v['id']] : 0;
            }
            $post = array_column($type_list, 'num');
            array_multisort($post, SORT_DESC, $type_list);

            $data =ListHelper::getRatio($type_list,'num','id','rate',0);
        }
        $this->success(__('获取成功'),['list'=>$data,'total'=>array_sum(array_column($data,'num'))]);
    }

}
